package com.xuecheng.content.service.iml;

import cn.hutool.core.bean.BeanUtil;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.mapper.CoursePublishPreMapper;
import com.xuecheng.content.model.po.CoursePublishPre;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.service.CoursePublishPreService;
import com.xuecheng.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CoursePublishPreServiceImpl implements CoursePublishPreService {
    private final CourseBaseMapper courseBaseMapper;
    private final CourseBaseServiceIml courseBaseServiceIml;
    private final TeachplanServiceIml teachplanServiceIml;
    private final CourseMarketMapper courseMarketMapper;
    private final CourseTeacherMapper courseTeacherMapper;
    private final CoursePublishPreMapper coursePublishPreMapper;
    /**
     * 课程提交审核
     *
     * @param courseId
     */
    @Override
    public void commitAudit(Long courseId,Long companyId) {
        CoursePublishPre coursePublishPre = new CoursePublishPre();

//        查询课程基本信息、课程营销信息、课程计划信息等课程相关信息，整合为课程预发布信息。

        //查询课程基本信息
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        //对已提交审核的课程不允许提交审核。
        if(courseBase.getAuditStatus().equals("202003")){
            XueChengPlusException.cast("课程已提交审核，请勿在提交");
        }
        //本机构只允许提交本机构的课程。
        if(!courseBase.getCompanyId().equals(companyId)){
            XueChengPlusException.cast("只允许提交本机构的课程");
        }
        //没有上传图片不允许提交审核。
        if(courseBase.getPic().isEmpty()){
            XueChengPlusException.cast("不允许图片为空");
        }
        BeanUtil.copyProperties(courseBase, coursePublishPre);

        //查询营销信息并转为JSON格式
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
        String courseMarketJson = JsonUtil.objectTojson(courseMarket);
        coursePublishPre.setMarket(courseMarketJson);

        //查询课程计划信息并转为JSON
        List<TeachplanDto> teachplanDtos = teachplanServiceIml.queryTeachplanTreeNodes(courseId);
        //没有添加课程计划不允许提交审核。
        if(teachplanDtos==null||teachplanDtos.size()==0){
            XueChengPlusException.cast("该课程没有添加课程计划");
        }
        String teachplanDtosJson = JsonUtil.listTojson(teachplanDtos);
        coursePublishPre.setTeachplan(teachplanDtosJson);

        //查询教师信息并转为JSON
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(courseId);
        String courseTeacherJson = JsonUtil.objectTojson(courseTeacher);
        coursePublishPre.setTeachers(courseTeacherJson);

        //设置提交时间
        coursePublishPre.setCreateDate(LocalDateTime.now());
        //设置预发布记录状态,已提交
        coursePublishPre.setStatus("202003");
        //机构id
        coursePublishPre.setCompanyId(companyId);
        //向课程预发布表course_publish_pre插入一条记录，如果已经存在则更新，审核状态为：已提交。
        //查询预发布表是否存在
        CoursePublishPre publishPre = coursePublishPreMapper.selectById(courseId);
        if(publishPre==null){
            coursePublishPreMapper.insert(coursePublishPre);
        }else {
            coursePublishPreMapper.updateById(coursePublishPre);
        }
        //更新基本表的审核状态
        courseBase.setAuditStatus("202003");
        courseBaseMapper.updateById(courseBase);
    }

}
